// import parseTime, formatTime and set to filter
export { parseTime, formatTime } from '@/utils'

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

/**
 * @param {number} time
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

export function timeDuration(start, end) {
  if (!end) end = new Date();

  const between = end.getTime() / 1000 - start.getTime() / 1000;
  if (between < 3600) {
    return parseInt(between / 60) + '分'
  } else {
    return parseInt(between / 3600) + '时' + parseInt(between % 3600 / 60) + '分'
  }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}

const AC_INPUT_OVER_VOLTAGE                   = 1 << 0;       // 交流输入过压
const AC_INPUT_UNDER_VOLTAGE                  = 1 << 1;       // 交流输入欠压
const AC_INPUT_OVER_VOLTAGE_DETACH            = 1 << 2;       // 交流输入过压脱离
const DC_OUTPUT_OVER_VOLTAGE                  = 1 << 6;       // 直流输出过压
const DC_OUTPUT_OVER_VOLTAGE_SHUTDOWN         = 1 << 7;       // 直流输出过压关机
const DC_OUTPUT_UNDER_VOLTAGE                 = 1 << 8;       // 直流输出欠压
const FAN_ERROR                               = 1 << 9;       // 风扇故障
const ENV_OVER_TEMPERATURE_PROTECT            = 1 << 12;      // 环境温度过温保护
const PFC_OVER_TEMPERATURE_PROTECT1           = 1 << 14;      // PFC过温保护1
const PFC_OVER_TEMPERATURE_PROTECT2           = 1 << 15;      // PFC过温保护2
const DC_OVER_TEMPERATURE_PROTECT1            = 1 << 16;      // 16: DC过温保护1
const DC_OVER_TEMPERATURE_PROTECT2            = 1 << 17;      // 17：DC过温保护2
const DUAL_EDGE_COMM_ERROR                    = 1 << 18;      // 18：原副边通信故障
const PRIMARY_EDGE_ERROR                      = 1 << 20;      // 20: 原边故障
const SECONDARY_EDGE_ERROR                    = 1 << 21;      // 21: 副边故障
const POWER_ON_STATE                          = 1 << 25;      // 开关机状态

const ERRORS = {
  [AC_INPUT_OVER_VOLTAGE          ]   : '交流输入过压',
  [AC_INPUT_UNDER_VOLTAGE         ]   : '交流输入欠压',
  [AC_INPUT_OVER_VOLTAGE_DETACH   ]   : '交流输入过压脱离',
  [DC_OUTPUT_OVER_VOLTAGE         ]   : '直流输出过压',
  [DC_OUTPUT_OVER_VOLTAGE_SHUTDOWN]   : '直流输出过压关机',
  [DC_OUTPUT_UNDER_VOLTAGE        ]   : '直流输出欠压',
  [FAN_ERROR                      ]   : '风扇故障',
  [ENV_OVER_TEMPERATURE_PROTECT   ]   : '环境温度过温保护',
  [PFC_OVER_TEMPERATURE_PROTECT1  ]   : 'PFC过温保护1',
  [PFC_OVER_TEMPERATURE_PROTECT2  ]   : 'PFC过温保护2',
  [DC_OVER_TEMPERATURE_PROTECT1   ]   : 'DC过温保护1',
  [DC_OVER_TEMPERATURE_PROTECT2   ]   : 'DC过温保护2',
  [DUAL_EDGE_COMM_ERROR           ]   : '原副边通信故障',
  [PRIMARY_EDGE_ERROR             ]   : '原边故障',
  [SECONDARY_EDGE_ERROR           ]   : '副边故障'
};

export function getAgingError(code) {
  if (!code) return '-'

  for (let key of Object.keys(ERRORS)) {
    if ((key & code) > 0) {
      return ERRORS[key];
    }
  }

  return '-'
}

// 不要使用4（4代表原来的已下机，目前使用is_removed标记来表示已下机）
const statusMap = {
  1: '已扫码',
  2: '老化中',
  3: '老化完成',
  9: '老化错误',
  5: '手动终止'
}
export function statusFilter(status) {
  if (!status) return '通道空闲'

  return statusMap[status]
}
